#pragma once

#include <thread>
#include <atomic>

#include "blockqueue.h"
#include "executor.h"


class ThreadExecutor : public Executor
{
public:
    ThreadExecutor();

    ThreadExecutor(const ThreadExecutor &b) = delete;

    ThreadExecutor(ThreadExecutor &&b) = delete;

    ThreadExecutor &operator=(const ThreadExecutor &) = delete;

    ThreadExecutor &operator=(ThreadExecutor &&) = delete;

    virtual ~ThreadExecutor();

    virtual void post(Task &&task) override;

private:
    void loop();

private:
    using TaskQueue = BlockQueue<std::function<void()>>;
    
    std::atomic_bool stopped_ = false;
    std::thread thread_;
    
    BlockQueue<std::function<void()>> queue_;
};
